package com.yyr.spring_boot.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class MyAop {

    public final static Logger LOGGER = LoggerFactory.getLogger(MyAop.class);

    @Pointcut("execution(public * com.yyr.spring_boot.modules.*.controller.*.*(..))")
    // order 数字越小，优先级越高
    @Order(1)
    public void controllerPointcut() {};

    @Pointcut("@annotation(com.yyr.spring_boot.aop.MyAnnotation)")
    @Order(1)
    public void annotationPointcut() {};

    @Before(value = "controllerPointcut()")
    public void beforeControllerAop(JoinPoint joinPoint) {
        LOGGER.debug("==== Before controller ====");
        ServletRequestAttributes attr =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attr.getRequest();
        LOGGER.debug(String.format("uri: %s", request.getServletPath()));
        LOGGER.debug(String.format("uri: %s", request.getMethod()));
        LOGGER.debug(String.format("uri: %s", joinPoint.getSignature().getName()));
    }

    @Around(value = "controllerPointcut()")
    public Object aroundControllerAop(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        LOGGER.debug("==== Around controller ====");
        return proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
    }

    @After(value = "controllerPointcut()")
    public void AfterControllerAop(JoinPoint joinPoint) {
        LOGGER.debug("==== After controller ====");
    }

    @Around(value = "annotationPointcut()")
    public Object aroundAnnotationAop(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object obj = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
        long end = System.currentTimeMillis();
        LOGGER.debug(String.format("Call method time: %s", (end - start)));
        return obj;
    }
}
